<?php
// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/ci_types/tcms_ci_article_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_type_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_ci_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpt/vpage_abs_dbc.class.php');


class VPage_news_article_DBC extends VPage_Abs_DBC {
	private $title = '';
	private $content = '';
	private $intro = '';
	private $meta_description = '';

	function __construct() {

	}


	public function save(DB_Connection $dbc, $current_user_id = 0) {
		$ret = false;

		if ($this->get_id()) {
			$vpage = new TCMS_VPage_DBC();
			if ($vpage->load_by_id($dbc, $this->get_id())) {


				// Saving content
				$vpage_ci = $this->load_content_item($dbc, $this->get_vpage_id(), 'article');
				if (!$vpage_ci) {
					$vpt_ci = new TCMS_VPT_CI_DBC();
					if ($vpt_ci->load_by_vpage_and_hid($dbc, $this->get_vpage_id(), 'article')) {
						$ci_news_article_ci = new TCMS_CI_Article_DBC();
						$ci_news_article_ci->get_title()->set_value($this->get_title());
						$ci_news_article_ci->get_content()->set_value($this->get_content());
						$ci_news_article_ci->get_intro()->set_value($this->get_intro());
						if ($ci_news_article_ci->save($dbc, $current_user_id)) {
							$vpage_ci = new TCMS_VPage_CI_DBC();
							$vpage_ci->set_t_ci_id($vpt_ci->get_id());
							$vpage_ci->set_type($ci_news_article_ci->get_ci()->get_type());
							$vpage_ci->set_vpage($this->get_vpage_id());
							$vpage_ci->set_ci_id($ci_news_article_ci->get_id());
							$vpage_ci->save($dbc);
						}
					}
				} else {
					$ci_news_article_ci = new TCMS_CI_Article_DBC();
					if ($ci_news_article_ci->load_by_id($dbc, $vpage_ci->get_ci_id())) {
						$ci_news_article_ci->get_title()->set_value($this->get_title());
						$ci_news_article_ci->get_content()->set_value($this->get_content());
						$ci_news_article_ci->get_intro()->set_value($this->get_intro());
						$ci_news_article_ci->save($dbc, $current_user_id);
					}
				}

				TCMS_VPage_DBC::update_last_modified($dbc, $vpage->get_id(), $current_user_id);

				$ret = true;
			}
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$content_ci = $this->load_content_item($dbc, $id, 'article');
		if ($content_ci) {
			$ci_news_article_ci = new TCMS_CI_Article_DBC();
			if ($ci_news_article_ci->load_by_id($dbc, $content_ci->get_ci_id())) {
				$this->set_title($ci_news_article_ci->get_title()->get_value());
				$this->set_content($ci_news_article_ci->get_content()->get_value());
				$this->set_intro($ci_news_article_ci->get_intro()->get_value());

			}
		}

		$this->set_vpage_id($id);

		return $id;
	}


	public function set_title($title) {
		$this->title = $title;
	}


	public function get_title() {
		return $this->title;
	}


	public function set_content($content) {
		$this->content = $content;
	}


	public function get_content() {
		return $this->content;
	}


	public function set_intro($intro) {
		$this->intro = $intro;
	}


	public function get_intro() {
		return $this->intro;
	}
}